#include"ringQueue.hpp"
#include<sys/types.h>
#include<unistd.h>
#include <cstdlib>
#include <ctime>

void* producer(void* args)
{
    RingQueue<int>* rq = (RingQueue<int>*)args;
    int x = rand()%100+1;
    while(true)
    {
        std::cout<<"生产："<<x<<std::endl;
        rq->push(x);
    }
}

void* consumer(void* args)
{
    RingQueue<int>* rq = (RingQueue<int>*)args;
    int x;
    while(true)
    {
        sleep(1);
        rq->pop(&x);
        std::cout<<"消费："<<x<<std::endl;
    }

}

int main()
{
    srand((uint64_t)time(nullptr) ^ getpid());
    RingQueue<int>* rq = new RingQueue<int>(5);
    pthread_t c[2],p[2];
    pthread_create(c,nullptr,consumer,(void*)rq);
    pthread_create(c+1,nullptr,consumer,(void*)rq);
    pthread_create(p,nullptr,producer,(void*)rq);
    pthread_create(p+1,nullptr,producer,(void*)rq);

    pthread_join(c[0],nullptr);
    pthread_join(c[1],nullptr);
    pthread_join(p[0],nullptr);
    pthread_join(p[1],nullptr);

    return 0;
}
